package com.ruoyi.common.utils.weather;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.httpclient.HttpClientUtil;
import com.ruoyi.common.utils.httpclient.HttpConstant;
import com.ruoyi.common.utils.spring.SpringUtils;

/**
 * @author zyj
 * @date 2022-08-22 19:27
 */
public class WeatherUtils {

    public static final String WEATHER_URL = "http://t.weather.sojson.com/api/weather/city/";

    /**
     * 获取今天天气
     */
    public static WeatherDto getWeather(String addressCode){
        if(addressCode==null){
            return new WeatherDto("","","","","");
        }
        String data = HttpClientUtil.sendHttpGet(WEATHER_URL + addressCode, HttpConstant.REQ_TIMES);
        JSONObject jsonObject= JSON.parseObject(data);
        try {
            JSONObject today = jsonObject.getJSONObject("data").getJSONArray("forecast").getJSONObject(0);
            return new WeatherDto(today.getString("week"),today.getString("type"),today.getString("low"),today.getString("high"),today.getString("notice"));
        } catch (Exception e) {
            e.printStackTrace();
            return new WeatherDto("","","","","");
        }
    }
}
